package file;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

public class FileSenderClient {
    private JFrame frame;
    private JTextField serverIPField;
    private JButton selectFileButton;
    private JButton sendButton;
    private JLabel statusLabel;
    private File selectedFile;

    public FileSenderClient() {
        // 创建GUI
        frame = new JFrame("局域网文件传输工具");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 200);
        frame.setLayout(new GridLayout(4, 2, 10, 10));

        // 服务器IP输入
        frame.add(new JLabel("服务器IP地址:"));
        serverIPField = new JTextField("localhost");
        frame.add(serverIPField);

        // 文件选择
        frame.add(new JLabel("选择文件:"));
        JPanel filePanel = new JPanel(new BorderLayout());
        selectFileButton = new JButton("浏览...");
        filePanel.add(selectFileButton, BorderLayout.CENTER);
        frame.add(filePanel);

        // 发送按钮
        frame.add(new JLabel());
        sendButton = new JButton("发送文件");
        sendButton.setEnabled(false);
        frame.add(sendButton);

        // 状态标签
        statusLabel = new JLabel("准备就绪", JLabel.CENTER);
        frame.add(statusLabel);
        frame.add(new JLabel());

        // 事件处理
        selectFileButton.addActionListener(e -> selectFile());
        sendButton.addActionListener(e -> sendFile());

        frame.setVisible(true);
    }

    private void selectFile() {
        JFileChooser fileChooser = new JFileChooser();
        int returnValue = fileChooser.showOpenDialog(frame);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            selectedFile = fileChooser.getSelectedFile();
            selectFileButton.setText(selectedFile.getName());
            sendButton.setEnabled(true);
            statusLabel.setText("已选择文件: " + selectedFile.getName());
        }
    }

    private void sendFile() {
        if (selectedFile == null) return;

        String serverIP = serverIPField.getText().trim();
        if (serverIP.isEmpty()) {
            JOptionPane.showMessageDialog(frame, "请输入服务器IP地址", "错误", JOptionPane.ERROR_MESSAGE);
            return;
        }

        new Thread(() -> {
            try {
                sendButton.setEnabled(false);
                selectFileButton.setEnabled(false);
                statusLabel.setText("正在连接服务器...");

                Socket socket = new Socket(serverIP, 12346);
                DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
                DataInputStream dis = new DataInputStream(socket.getInputStream());

                // 发送文件名和大小
                dos.writeUTF(selectedFile.getName());
                dos.writeLong(selectedFile.length());

                // 发送文件内容
                statusLabel.setText("正在发送文件: 0%");
                try (FileInputStream fis = new FileInputStream(selectedFile)) {
                    byte[] buffer = new byte[4096];
                    int read;
                    long totalSent = 0;
                    long fileSize = selectedFile.length();

                    while ((read = fis.read(buffer)) != -1) {
                        dos.write(buffer, 0, read);
                        totalSent += read;

                        // 更新进度
                        int percent = (int)((totalSent * 100) / fileSize);
                        final int displayPercent = percent;
                        SwingUtilities.invokeLater(() ->
                                statusLabel.setText("正在发送文件: " + displayPercent + "%"));
                    }
                }

                // 接收服务器响应
                String response = dis.readUTF();
                statusLabel.setText(response);

                socket.close();
            } catch (IOException e) {
                statusLabel.setText("错误: " + e.getMessage());
                e.printStackTrace();
            } finally {
                sendButton.setEnabled(true);
                selectFileButton.setEnabled(true);
            }
        }).start();
    }

    public static void main(String[] args) {
        new FileSenderClient();
    }
}
